package com.mjw.tender.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mjw.tender.api.ArticleService;
import com.mjw.tender.domain.Article;
import com.mjw.tender.domain.Favourite;
import com.mjw.tender.mapper.ArticleMapper;
import com.mjw.tender.mapper.FavouriteMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service(version = "1.0.0")
public class ArticleServiceImpl implements ArticleService {

    @Resource
    ArticleMapper articleMapper;

    @Override
    public List<Article> selectListByArticle(Article article, Integer currPage, Integer limit) {
        Page page = PageHelper.startPage(currPage,limit);
        List<Article> list = new ArrayList<Article>(articleMapper.selectListByArticle(article));
        return list;
    }

    /**
     * 涉及并发写
     * 浏览次数不重要，允许部分丢失。也可以使用分布式锁
     */
    @Override
    @Transactional
    public Article read(Long id){
        articleMapper.increaseBrowse(id);
        return articleMapper.selectByPrimaryKey(id);
    }

    @Override
    public Article get(Long articleId) {
        return articleMapper.selectByPrimaryKey(articleId);
    }

    @Override
    public Long add(Article article) {
        articleMapper.insertSelective(article);
        return article.getId();
    }


    @Override
    public int update(Article article) {
        return articleMapper.updateByPrimaryKeySelective(article);
    }

    @Override
    public List<Article> getLikeArticle(Long userId) {
        return articleMapper.getLikeArticle(userId);
    }


    @Override
    public List<Article> getCollectArticle(Long userId) {
        return articleMapper.getCollectArticle(userId);
    }

    @Override
    public List<Article> getHotBrowseList(Integer num) {
        return articleMapper.selectOrderByBrowse(num);
    }



}
